package com.yuluke.ylsaudiorecorder.play;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.yuluke.ylsaudiorecorder.R;

import java.util.Locale;

public class PlayActivity extends AppCompatActivity {

    public static final String EXTRA_FILE_NAME = "file_name";
    public static final String EXTRA_FILE_PATH = "file_path";
    YlsPlayer mPlayer;
    ProgressBar mPb;

//    private
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_play);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        TextView tv = findViewById(R.id.text_playing_file_name);
        mPb = findViewById(R.id.progress_play);

        mPlayer = new YlsPlayer(this);

        Intent intent = getIntent();
        tv.setText(intent.getStringExtra(EXTRA_FILE_NAME));

        findViewById(R.id.button_play_start).setOnClickListener( v -> {
            mPlayer.start(intent.getStringExtra(EXTRA_FILE_PATH));
            mPlayer.setProgressListener(new YlsPlayer.OnProgressListener() {
                @Override
                public void onProgress(int total, int progress) {
                    mPb.setMax(total);
                    mPb.setProgress(progress);
                }
            });
        });

        Locale.getDefault();
    }
}